<?php
 error_reporting(E_ALL); 
 set_time_limit(0); 
 
 //echo "\n====== Starting thread 1 ======\n";

 if (empty($_SERVER['DOCUMENT_ROOT']) == true)
 {
	$_SERVER['DOCUMENT_ROOT'] = '/var/www/sigserv';
 }
 
 $_q = Array();
 $_q_n = '/queue/root/p';
 $_ii = 100;
 
 while ($_ii > -1)
 {
	$_q[$_ii] = $_q_n . $_ii;
	
	$_ii--;
 }
 
  
 //подготовка
  
 set_include_path(  get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] . '/lib');
 
 require_once('Zend/Registry.php');
 require_once('Zend/Loader/Autoloader.php');
 require_once('Rediska.php');
 
 require_once('Signalsy/Exceptions.php');
 
 $loader = Zend_Loader_Autoloader::getInstance();

 $loader->registerNamespace('Signalsy');

 $config = parse_ini_file('config.ini', TRUE);
 
  
 //поправка для timezone
 date_default_timezone_set($config['Default']['use_default_timezone']);
 
 //sleep(rand(1,3));
 
 $sigQ = Signalsy_MQ::getInstance(); //array('log' => new Zend_Log(new Zend_Log_Writer_Stream('/var/log/signalsy_mq_test.log'))));
 
 
 //echo "Test wildcard symbols at queue\n";
 
 //var_dump($sigQ->getRediska()->info());
 
 
 //var_dump($sigQ->getRediska()->getQueues());
 
 
 //var_dump($sigQ->getQueuesByPattern('/queue/root/p*'));
 
 //var_dump($sigQ->getQueuesByPattern('/queue/root/p?'));
 
 
 
 
 
 $q_name = $_q[rand(0, 100)];
 
 //echo "[Start] Queue: ". $q_name ."\n";
 
 $ex = $sigQ->isExists($q_name);
// echo "[Debug] Checking queue exists: "; 

// if ($ex === false) { echo " false!\n"; } else  { echo " true \n"; }

 if ($ex === true)
 {
//	echo "Queue isExist, with " . $sigQ->count($q_name) . " \n";
	//$sigQ->deleteQueue($q_name);
 }
 else
 { 
//	echo "Creating new queue \n";
	$res = $sigQ->createQueue($q_name);
	
	if ($res === false)
	{
		die('Fuck, not created queue');
	}
	
 }
 
 
 
 
// 
 
// $sigQ->deleteQueue($q_name);
 
// var_dump($sigQ->isExists($q_name));
 
// $res = $sigQ->createQueue($q_name);
// var_dump($sigQ->isExists($q_name));

 
 $_max = 300;
 $_c = $_max;
 
 
 $st = xdebug_time_index();
 
 while ($_c > 0)
 {
// 	echo '[Sending] Messages: ' . $_c . "\n";   //  q_name
 	
 	$mess = array('type' => 'time_messages', 'channel' => $q_name, 'body' => 'At Now: ' . date('r'), 'timestamp' => time()); 
 	
 	$sigQ->send($mess);
 	
 	$_c--; 	
 }
 
 $ft = xdebug_time_index();
 /*
 $rinfo = $sigQ->getRediska()->info();  // used_memory
 
 $master = null;
 
 foreach ($rinfo as  )
 
 var_dump($rinfo);
 */
 //   ceil($rinfo['used_memory']/1024). ' Kb; '
 echo "[".$q_name."] Total: " . $_max . " mes. Time: " . round(($ft - $st), 4) . " sec, " . round(($ft - $st)/$_max, 4) . "/mes., " . round(1/(($ft - $st)/$_max), 1) . " m/sec; \n";

 

 
// $sigQ->getRediska()->flushDb(true);
// $sigQ->getRediska()->save(true);
 
 
 // echo "Messages per second: " .  . "\n\n";
 
// echo "Now, In queue: " . $sigQ->count($q_name) ."\n";
 
 //var_dump($sigQ->receive($q_name, 3));
 
// echo "[Debug] Memory: " . ceil(xdebug_memory_usage()/1024) . " Kb\n";
// echo "\n====== Finishing thread 1 ======\n";
 

 